package leecoke;

public class Test092 {
    public static class ListNode{
        int val;
        ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }
    public static ListNode reverseBetween(ListNode head, int left, int right) {
        // 只有一个节点，或者左右范围一致直接返回
        if (head.next == null || left == right) {
            return head;
        }
        // 将链表分为三段
        int size = 0;
        ListNode preB = null;
        ListNode preE = null;
        ListNode inB = null;
        ListNode inE = null;
        ListNode posB = null;
        ListNode posE = null;
        ListNode next = null;
        while (head != null){
            size++;
            next = head.next;
            // 注意需要打断后续
            head.next = null;
            // 注意例子中 left是从 1 开始计算的
            if(size < left){
                if (preB == null) {
                    preB = head;
                    preE = head;
                } else {
                    preE.next = head;
                    preE = head;
                }
            } else if (size >= left && size <= right) {
                if (inB == null) {
                    inB = head;
                    inE = head;
                } else {
                    inE.next = head;
                    inE = head;
                }
            } else {
                if (posB == null) {
                    posB = head;
                    posE = head;
                } else {
                    posE.next = head;
                    posE = head;
                }
            }
            head = next;
        }
        // 有需要反转的链表
        if (inB != null) {
            ListNode[] listNodes = reverserNode(inB);
            inB = listNodes[0];
            inE = listNodes[1];
        }
        // left != 1 时，左部分有节点
        if (preE != null) {
            // 左部分尾结点的next 指向 中间部分的头节点
            preE.next = inB;
            // 确定连接右部分的节点
            // 如果中间部分没有节点，则使用左部分节点去连右部分头节点
            // 中间有节点，使用中间部分的尾节点去连接右部分头节点
            inE = inE == null ? preE : inE;
        }
        // 连接右部分
        if (inE != null) {
            inE.next = posB;
        }
        // 左部分尾结点尾不空，则返回开始节点为左部分头节点
        // 左部分为空；则判断中间部分是否为空
        return preE != null ? preB : (inB != null ? inB : posB);
    }

    public static ListNode[] reverserNode(ListNode head) {
        ListNode temp = head;
        ListNode pre = null;
        ListNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return new ListNode[]{pre, temp};
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        printNode(head);
        ListNode listNode = reverseBetween(head, 2, 4);
        printNode(listNode);
    }

    public static void printNode(ListNode node) {
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }
}
